
package com.scalar.chiptrack.tags;

import java.io.IOException;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.struts.taglib.html.LinkTag;

import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.ConfigManager;



public class RestrictedLinkTag extends LinkTag {
    
    private boolean m_showLink = false;
    
    /**
     * Functioanlity attribute
     */
    private String m_functionality = null;
    
    public String getFunctionality() {
        return m_functionality;
    }
    
    public void setFunctionality (String functionality) {
        m_functionality = functionality;
    }
    
    /**
     * Action Property
     */
    private String m_action = User.USER_ACTION_VIEW;
    
    public String getAction() {
        return m_action;
    }
    
    public void setAction (String action) {
        if (action != null) {
            m_action = action;
        }
    }
    
    
    /**
     * Render the beginning of the hyperlink.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        // Dont show the link if its not allowed
        m_showLink = showLink();
        if (m_showLink == false) {
            return (EVAL_BODY_BUFFERED);
        }
        
        // Generate the opening anchor element
        StringBuffer results = new StringBuffer("<a href=\"");
        
        String hrefString = calculateURL();
        if ((hrefString != null) && (hrefString.startsWith("/chiptrack"))) {            
            hrefString = "/" + ConfigManager.getChipTracContext() + hrefString.substring(10);
            //System.out.println ("href string = " + hrefString);
        }
        
        results.append(hrefString);
        results.append("\"");
        if (target != null) {
            results.append(" target=\"");
            results.append(target);
            results.append("\"");
        }
        results.append(prepareStyles());
        results.append(prepareEventHandlers());
        results.append(">");

        // Print this element to our output writer
        try {
            JspWriter out = pageContext.getOut();
            out.println (results.toString());
        }
        catch (IOException ioe) {
            System.out.println ("WARNING!! Exception while rendering \"RestrictedLink\" Tag. \n" + ioe);
        }
        
        // Evaluate the body of this tag
        this.text = null;
        return (EVAL_BODY_BUFFERED);
    }
    

    /**
     * Save the associated label from the body content.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doAfterBody() throws JspException {

        if (bodyContent != null) {
            String value = bodyContent.getString().trim();
            if (value.length() > 0)
                text = value;
        }
        
        return (SKIP_BODY);
    }
    
    
    /**
     * Render the end of the hyperlink.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {

        // Prepare the textual content and ending element of this hyperlink
        StringBuffer results = new StringBuffer();
        
        if (text != null) {          
            results.append(text);
        }
        
        // Dont show the link if its not allowed
        if (m_showLink != false) {
            results.append ("</a>");
        }

        // Render the remainder to the output stream
        try {
            JspWriter out = pageContext.getOut();
            out.print (results.toString());
        }
        catch (IOException ioe) {
            System.out.println ("WARNING!! Exception while rendering \"RestrictedLink\" Tag. \n" + ioe);
        }
        
        // Evaluate the remainder of this page
        return (EVAL_PAGE);
    }

    
    private boolean showLink() {
        
        if (m_functionality == null) {
            return false;
        }
        
        HttpSession session = pageContext.getSession();
        if (session == null) {
            return false;
        }

        User user = (User) session.getAttribute (SessionKeys.USER_KEY);
        if (user == null) {
            return false;
        }
        
        return user.isActionAllowed (m_functionality, m_action);
    }
    
    
}